/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.front.web.index;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.CommonParameter;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.advertising.service.AdvertisingPohotoService;
import com.thinkgem.jeesite.modules.product.entity.product.GenProduct;
import com.thinkgem.jeesite.modules.product.service.catalog.GenCatalogService;
import com.thinkgem.jeesite.modules.product.service.product.GenProductService;

/**
 * 产品显示Controller（FRONT）
 * 
 * @author Egan
 * @version 2016-12-15
 */
@Controller
@RequestMapping(value = "${frontPath}/front/index")
public class IndexController extends BaseController {

	@Autowired
	GenProductService genProductService;
	
	@Autowired
	GenCatalogService genCatalogService;

	@Autowired
	AdvertisingPohotoService advertisingPohotoService;

	@ModelAttribute
	public GenProduct get(@RequestParam(required=false) String id) {
		System.out.println("========== IndexController get ==========");
		System.out.println("id : " + id);
		
		GenProduct entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = genProductService.get(id);
		}
		if (entity == null){
			entity = new GenProduct();
		}
		return entity;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(GenProduct genProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		System.out.println("========== IndexController list ==========");
		
		
		//精选商品
        genProduct.setCatalogName("");
		Page<GenProduct> genProductsKFBNew = new Page<GenProduct>(request, response);
		genProductsKFBNew = genProductService.findListByIsNew(genProductsKFBNew, genProduct);
		model.addAttribute("page1", genProductsKFBNew);
		
		//配件
        genProduct.setCatalogName(CommonParameter.namePJ);
		Page<GenProduct> genProductsPJ = new Page<GenProduct>(request, response);
		genProductsPJ = genProductService.findListByCatalogName(genProductsPJ, genProduct);
		model.addAttribute("page2", genProductsPJ);
		model.addAttribute("uploadPath", CommonParameter.uploadPath);
		
		model.addAttribute("uploadPath", CommonParameter.uploadPath);
        model.addAttribute("advertisingPohoto", advertisingPohotoService.getTop());

		return "modules/index/index";
	}

}